﻿package mortal.common.swfPlayer.data
{
    import com.gengine.debug.*;
    import flash.display.*;
    import flash.utils.*;
    import mortal.common.swfPlayer.frames.*;

    public class ActionInfo extends Object
    {
        public var swf:SwfFrames;
        public var action:int;
        public var startFrame:int;
        public var endFrame:int;
        public var frameRate:int;
        public var delay:int;
        public var dir:int;
        public var isInitEmptyFrames:Boolean = false;
        protected var _framesMap:Dictionary;
        private var _isCreateFrames:Boolean = false;

        public function ActionInfo(param1:int, param2:int, param3:int, param4:int, param5:int, param6:int = 8)
        {
            this.action = param1;
            this.startFrame = param2;
            this.endFrame = param3;
            this.frameRate = param4;
            this.delay = param5;
            this.dir = param6;
            this._framesMap = new Dictionary();
            return;
        }// end function

        public function get isCreateFrames() : Boolean
        {
            return this._isCreateFrames;
        }// end function

        public function set movieClip(param1:MovieClip) : void
        {
            if (param1)
            {
                if (this._isCreateFrames)
                {
                    this.setActionMovieClip(param1);
                }
                else if (this.dir == 8)
                {
                    this.create8Action(param1);
                    this._isCreateFrames = true;
                }
                else if (this.dir == 1)
                {
                    this.createOneDirAction(param1);
                    this._isCreateFrames = true;
                }
                else
                {
                    this.createXDirAction(param1);
                    this._isCreateFrames = true;
                }
            }
            else if (this._isCreateFrames == false)
            {
                if (this.dir == 8)
                {
                    this.create8Action(param1);
                    this._isCreateFrames = true;
                }
                else if (this.dir == 1)
                {
                    this.createOneDirAction(param1);
                    this._isCreateFrames = true;
                }
                else
                {
                    this.createXDirAction(param1);
                    this._isCreateFrames = true;
                }
            }
            return;
        }// end function

        public function getActionDir(param1:int) : FrameArray
        {
            return this._framesMap[param1];
        }// end function

        public function setActionMovieClip(param1:MovieClip) : void
        {
            var _loc_2:FrameArray = null;
            var _loc_3:int = 0;
            var _loc_4:BitmapFrame = null;
            var _loc_5:BitmapFrame = null;
            if (this.dir == 8)
            {
                _loc_3 = 1;
                while (_loc_3 <= 8)
                {
                    
                    _loc_2 = this._framesMap[_loc_3];
                    if (_loc_2)
                    {
                        for each (_loc_4 in _loc_2)
                        {
                            
                            _loc_4.swf = this.swf;
                        }
                    }
                    _loc_3++;
                }
            }
            else if (this.dir == 1)
            {
                _loc_2 = this._framesMap[DirectionType.DefaultDir];
                if (_loc_2)
                {
                    for each (_loc_5 in _loc_2)
                    {
                        
                        _loc_5.swf = this.swf;
                    }
                }
            }
            return;
        }// end function

        protected function createAction(param1:MovieClip) : void
        {
            return;
        }// end function

        private function createOneDirAction(param1:MovieClip) : void
        {
            var _loc_3:BitmapFrame = null;
            if (param1)
            {
                if (this.endFrame > param1.totalFrames)
                {
                    this.endFrame = param1.totalFrames;
                    if (this.startFrame > this.endFrame)
                    {
                        return;
                    }
                }
            }
            var _loc_2:* = new FrameArray(this);
            var _loc_4:* = this.startFrame;
            while (_loc_4 <= this.endFrame)
            {
                
                _loc_3 = new BitmapFrame(_loc_4);
                _loc_3.swf = this.swf;
                _loc_2.push(_loc_3);
                _loc_4++;
            }
            this._framesMap[DirectionType.DefaultDir] = _loc_2;
            return;
        }// end function

        protected function create8Action(param1:MovieClip) : void
        {
            var _loc_2:BitmapFrame = null;
            var _loc_3:FrameArray = null;
            var _loc_4:int = 0;
            var _loc_5:int = -1;
            var _loc_6:* = this.startFrame;
            while (_loc_6 <= this.endFrame)
            {
                
                _loc_4 = Math.ceil((_loc_6 - this.startFrame + 1) / this.frameRate);
                if (_loc_4 != _loc_5)
                {
                    _loc_3 = this._framesMap[_loc_4];
                    if (_loc_3 == null)
                    {
                        var _loc_7:* = new FrameArray(this);
                        _loc_3 = new FrameArray(this);
                        this._framesMap[_loc_4] = _loc_7;
                    }
                    else
                    {
                        Log.system("方向已经存在");
                    }
                    _loc_5 = _loc_4;
                }
                _loc_2 = new BitmapFrame(_loc_6);
                _loc_2.swf = this.swf;
                _loc_3.push(_loc_2);
                _loc_6++;
            }
            _loc_3 = this._framesMap[DirectionType.SouthWest];
            _loc_3 = _loc_3.clone();
            _loc_3.isTurn = true;
            this._framesMap[DirectionType.SouthEast] = _loc_3;
            _loc_3 = this._framesMap[DirectionType.West];
            _loc_3 = _loc_3.clone();
            _loc_3.isTurn = true;
            this._framesMap[DirectionType.East] = _loc_3;
            _loc_3 = this._framesMap[DirectionType.NorthWest];
            _loc_3 = _loc_3.clone();
            _loc_3.isTurn = true;
            this._framesMap[DirectionType.NorthEast] = _loc_3;
            return;
        }// end function

        protected function createXDirAction(param1:MovieClip) : void
        {
            var _loc_2:BitmapFrame = null;
            var _loc_3:FrameArray = null;
            var _loc_4:int = 0;
            var _loc_5:int = -1;
            var _loc_6:Array = [];
            var _loc_7:* = this.startFrame;
            while (_loc_7 <= this.endFrame)
            {
                
                _loc_4 = Math.ceil((_loc_7 - this.startFrame + 1) / this.frameRate);
                if (_loc_4 != _loc_5)
                {
                    _loc_3 = this._framesMap[_loc_4];
                    if (_loc_3 == null)
                    {
                        var _loc_9:* = new FrameArray(this);
                        _loc_3 = new FrameArray(this);
                        this._framesMap[_loc_4] = _loc_9;
                    }
                    else
                    {
                        Log.system("方向已经存在");
                    }
                    _loc_5 = _loc_4;
                    _loc_6.push(_loc_4);
                }
                _loc_2 = new BitmapFrame(_loc_7);
                _loc_2.swf = this.swf;
                _loc_3.push(_loc_2);
                _loc_7++;
            }
            var _loc_8:Array = [];
            for each (_loc_4 in _loc_6)
            {
                
                _loc_5 = DirectionType.getXTurnDir(_loc_4);
                if (_loc_5 != -1 && _loc_6.indexOf(_loc_5) == -1)
                {
                    _loc_3 = this._framesMap[_loc_4];
                    _loc_3 = _loc_3.clone();
                    _loc_3.isTurn = true;
                    this._framesMap[_loc_5] = _loc_3;
                    _loc_6.push(_loc_5);
                    _loc_8.push(_loc_5);
                }
            }
            for each (_loc_4 in _loc_6)
            {
                
                _loc_5 = DirectionType.getYTurnDir(_loc_4);
                if (_loc_5 != -1 && _loc_6.indexOf(_loc_5) == -1)
                {
                    _loc_3 = this._framesMap[_loc_4];
                    _loc_3 = _loc_3.clone();
                    _loc_3.isTurnY = true;
                    this._framesMap[_loc_5] = _loc_3;
                    _loc_6.push(_loc_5);
                    if (_loc_8.indexOf(_loc_4) != -1)
                    {
                        _loc_3.isTurn = true;
                    }
                }
            }
            return;
        }// end function

        public function clone() : ActionInfo
        {
            var _loc_1:* = new ActionInfo(this.action, this.startFrame, this.endFrame, this.frameRate, this.delay, this.dir);
            return _loc_1;
        }// end function

        public function dispose() : void
        {
            var _loc_1:BitmapFrame = null;
            var _loc_2:FrameArray = null;
            var _loc_3:int = 0;
            for each (_loc_2 in this._framesMap)
            {
                
                _loc_3 = 0;
                while (_loc_3 < _loc_2.length)
                {
                    
                    _loc_1 = _loc_2[_loc_3];
                    _loc_1.dispose(true);
                    _loc_3++;
                }
            }
            return;
        }// end function

        public function getDirFrame(param1:int, param2:int = 0) : BitmapFrame
        {
            var _loc_3:* = this._framesMap[param1] as Array;
            if (_loc_3)
            {
                return _loc_3[param2];
            }
            return null;
        }// end function

    }
}
